package fr.utbm.gi.vi51.slizers.pw.gui.menu;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.lang.ref.WeakReference;

import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import fr.utbm.gi.vi51.slizers.pw.gui.LaunchSimulationListener;

/**
 * @author Adrien
 *
 * Implementation of a GUI Panel permitting to select some creatures in the {@link MenuPanel}
 * 
 */
public class CreatureSelectorPanel extends JPanel{

	/**
	 * Version number associated to the serializable class.
	 */
	private static final long	serialVersionUID	= 4955848735685474411L;
	
	/**
	 * The list containing all the creature Qlearners in the directory "current/creatures"
	 */
	private JList 	creaturesList;
	
	/**
	 * The listener on the creaturesList selected.
	 */
	private final WeakReference< creaturesSelectionListener >	menuPanel;
	
	/**
	 * Constructor
	 */
	public CreatureSelectorPanel( creaturesSelectionListener _gui )
	{
		this.menuPanel = new WeakReference< creaturesSelectionListener >( _gui );
		
		this.setLayout( new BorderLayout() );
		
		// Label for the list
		JLabel label = new JLabel("Select your creatures");
		this.add( label, BorderLayout.NORTH );
		
		// Set the list of creature
		this.creaturesList = new JList();
		this.creaturesList.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
		
		// Fill the list
		fillMapList();
		
		// Add a Listener to change the selection of creatures Nb button state 
		this.creaturesList.addListSelectionListener( new ListSelectionListener()
		{
			@Override
			public void valueChanged( ListSelectionEvent evt )
			{
				if( CreatureSelectorPanel.this.creaturesList.getSelectedValue() == "None" )
				{
					CreatureSelectorPanel.this.menuPanel.get().creatureSelectionChanged( true );
				}
				else
				{
					CreatureSelectorPanel.this.menuPanel.get().creatureSelectionChanged( false );
				}
			}
		});
		
		this.creaturesList.setSelectedIndex( 0 );
		
		// Add component in the layout
		this.add( this.creaturesList, BorderLayout.CENTER );
		// Add a scroll bar
		JScrollPane scroller = new JScrollPane( this.creaturesList );
		this.add( scroller, BorderLayout.CENTER );
	}
	
	/**
	 * Fill the list with the maps stored in the directory "current/creatures"
	 */
	public void fillMapList()
	{
		DefaultListModel creaturesDLM = new DefaultListModel();

		String creaturesDirectoryPath = System.getProperty( "user.dir" ).concat( "/creatures/" );  //$NON-NLS-1$//$NON-NLS-2$

		File creaturesDirectory = new File(creaturesDirectoryPath);
		
		File[] creaturesFiles = creaturesDirectory.listFiles();
		
		if( creaturesFiles == null )
			JOptionPane.showMessageDialog( this, "the creatures directory is not found\n" + //$NON-NLS-1$
					"Please resolve this problem and restart the application" ); //$NON-NLS-1$
		else
		{
			creaturesDLM.addElement( "None" );
			// Browse the map files and add the files
			for (int i = 0; i < creaturesFiles.length; i++)
			{
				if( creaturesFiles[i].getName().endsWith( ".slizers" ) )
				{
					creaturesDLM.addElement( creaturesFiles[i].getName() );
				}
			}		
			
			this.creaturesList.setModel(creaturesDLM);
		}
	}
	
	/**
	 * Returns the selected creature Qlearners path.
	 * 
	 * @return the selected creature Qlearners path.
	 */
	public String getSelectedCreaturesPath() 
	{
		return ( String )this.creaturesList.getSelectedValue();
	}
	
}
